package com.clh.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.clh.dao.FamilyTypeDao;
import com.clh.entity.FamilyType;
import com.clh.util.DBUtil;

/**
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 邱宏宇
 * @date 2022年5月26日 上午9:39:11
 * @version V1.0
 */
public class FamilyTypeDaoImpl implements FamilyTypeDao {

	/*
	 * @Description:获取家庭成员类型表
	 * 
	 * @return（List<FamilyType>）
	 */
	@Override
	public List<FamilyType> getFamilyTypeList() {
		// 创建list，若未获得对象返回值为null
		List<FamilyType> list = new ArrayList<FamilyType>();
		StringBuilder sql = new StringBuilder();
		sql.append("select familyType_no,").append(" familyType_name").append(" from family_type");

		ResultSet rs = DBUtil.query(sql.toString());
		try {
			while (rs.next()) {
				Integer familyTypeNo = rs.getInt("familyType_no");
				String familyTypeName = rs.getString("familyType_name");

				// 构建familyType对象
				FamilyType familyType = new FamilyType();
				familyType.setFamilyTypeNo(familyTypeNo);
				familyType.setFamilyTypeName(familyTypeName);

				list.add(familyType);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/*
	 * @Description:（增加家庭成员类型）
	 * 
	 * @return（boolean）
	 */
	@Override
	public boolean addFamilyType(String familyTypeName) {
		StringBuilder sql = new StringBuilder();
		sql.append("insert into family_type ").append(" (familyType_name)").append(" values ('").append(familyTypeName)
		.append("')");

		
		System.out.println("familytypedao:"+sql);
		try {
			int n = DBUtil.update(sql.toString());
			if (n > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean modifyFamilyTypeByNo(FamilyType familyType) {
		StringBuilder sql = new StringBuilder();
		sql.append("update family_type set familyType_name=?").append(" where familyType_no=?");

		// 获取所传对象参数
		Integer familyTypeNo = familyType.getFamilyTypeNo();
		String familyTypeName = familyType.getFamilyTypeName();
		
		try {
			int n = DBUtil.update(sql.toString(),familyTypeName, familyTypeNo);
			if(n>0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

}
